// 用户列表
let userList = async (ctx, next) => {

  let userData = await ctx.db.querys(`select * from user where status="ok"`)
  ctx.body = { code: 1, msg: '用户列表数据获取成功', data: userData }
}




// 用户列表模糊搜索
let searchUserList = async (ctx, next) => {
  const { name, type, startTime, endTime } = ctx.request.body;
  console.log(name, type, startTime, endTime)
  let userData = await ctx.db.querys(`select * from user where status="ok"`)
  if (!userData.length) {
    return ctx.body = { code: 0, msg: '暂无数据', data: [] }
  }
  if (name !== '') {
    userData = userData.filter(item => item.name.indexOf(name) !== -1)
  }
  if (type === 'createTime') {
    userData = userData.filter(item => item.createTime * 1 > startTime * 1 && item.createTime * 1 < endTime * 1)
  }
  if (type === 'loginTime') {
    userData = userData.filter(item => item.loginTime * 1 > startTime * 1 && item.loginTime * 1 < endTime * 1)
  }
  ctx.body = { code: 1, msg: '数据请求成功', data: userData }
}

// 冻结账号
let userIco = async (ctx, next) => {

  let isOpr = await ctx.isJudge(ctx, 'D');

  if (!isOpr) {
    ctx.body = { code: 0, msg: '您无权操作' }
    return
  }
  // 当前登录用户如果存在产出的权限 则可执行冻结
  const { userid } = ctx.request.body
  let dbReturn = await ctx.db.querys(`update user set status="freeze" where userid='${userid}'`)
  if (!dbReturn.affectedRows) {
    ctx.body = { code: 0, msg: '操作失败,请重新再来' }
    return
  }
  ctx.body = { code: 1, msg: '冻结成功' }
}



// *********黑名单
// 黑名单列表
let blackUserList = async (ctx, next) => {
  let userData = await ctx.db.querys(`select * from user where status="freeze"`)
  ctx.body = { code: 1, msg: '用户列表数据获取成功', data: userData }
}


// 黑名单列表模糊搜索
let searchBlackUserList = async (ctx, next) => {
  const { name } = ctx.request.body;
  let userData = await ctx.db.querys(`select * from user where status="freeze"`)

  if (!userData.length) {
    return ctx.body = { code: 0, msg: '暂无数据', data: [] }
  }

  if (name !== '') {
    userData = userData.filter(item => item.name.indexOf(name) !== -1)
  }

  ctx.body = { code: 1, msg: '数据请求成功', data: userData }

}


// 黑名单解除冻结
let blackUserIco = async (ctx, next) => {

  let isOpr = await ctx.isJudge(ctx, 'D');

  if (!isOpr) {
    ctx.body = { code: 0, msg: '您无权操作' }
    return
  }
  // 当前登录用户如果存在产出的权限 则可执行冻结
  const { userid } = ctx.request.body
  let dbReturn = await ctx.db.querys(`update user set status="ok" where userid='${userid}'`)
  if (!dbReturn.affectedRows) {
    ctx.body = { code: 0, msg: '操作失败,请重新再来' }
    return
  }
  ctx.body = { code: 1, msg: '解冻成功' }
}
// *********

// 点击编辑获取对应数据
let getSetUser = async (ctx, next) => {
  let isOpr = await ctx.isJudge(ctx, 'E');
  if (!isOpr) {
    ctx.body = { code: 0, msg: '您无权操作' }
    return
  }
  const { userid } = ctx.request.body;
  let userData = await ctx.db.querys(`select * from user where userid='${userid}'`)
  ctx.body = { code: 1, msg: '数据请求成功', data: userData }
}


// 编辑之后确定,修改数据库数据
let setUser = async (ctx, next) => {
  const { userid, pwd, roleid, status, name, age, sex, phone, address, jobTime } = ctx.request.body;
  let userData = await ctx.db.querys(`select * from user where userid='${userid}'`)
  if (!userData.length) {
    return ctx.body = { code: 0, msg: '未找到该数据', data: userData }
  }

  let password = userData[0].pwd
  if (pwd !== password) {
    await ctx.db.querys(`update user set isTrue='false' where userid='${userid}'`)
  }

  await ctx.db.querys(`update user set name='${name}',age='${age}',sex='${sex}',phone=${phone},address='${address}',jobTime='${jobTime}',pwd='${pwd}',roleid='${roleid}',status='${status}' where userid='${userid}'`)

  ctx.body = { code: 1, msg: '数据请求成功', data: [] }
}


// 添加账号
let addUser = async (ctx, next) => {
  let isOpr = await ctx.isJudge(ctx, 'A');

  // 判断当前用户是否可以操作
  if (!isOpr) {
    ctx.body = { code: 0, msg: '您无权操作' }
    return
  }
  // 如果权限够  就可以添加
  const { userName, pwd, roleid, status, name } = ctx.request.body;

  let userList = await ctx.db.querys(`select * from user where userName='${userName}'`)

  if (userList.length !== 0) {
    ctx.body = { code: 0, msg: '已存在该用户名,请更换用户名后重试' }
    return
  }


  let createTime = new Date().getTime()
  let userid = Math.floor(Math.random() * 99999999)
  let results = await ctx.db.querys(`insert into user (userid,userName,pwd,roleid,status,createTime,isTrue,name) values ('${userid}','${userName}','${pwd}','${roleid}','${status}','${createTime}','true',"${name}")`)

  if (results.affectedRows === 1) {
    ctx.body = { code: 1, msg: '添加成功' }
    return
  }
  ctx.body = { code: 0, msg: '添加失败' }
}


let setPwd = async (ctx, next) => {
  const { userid, newPwd } = ctx.request.body;
  let userData = await ctx.db.querys(`select * from user where userid=${userid}`)
  if (!userData.length) {
    ctx.body = { code: 0, msg: '数据请求失败' }
    return
  }


  await ctx.db.querys(`update user set pwd='${newPwd}',isTrue="false" where userid='${userid}'`)


  ctx.body = { code: 1, msg: "密码修改成功,请重新登陆" }

}


export default {
  userList, searchUserList, userIco, getSetUser, setUser, addUser, blackUserList, searchBlackUserList, blackUserIco, setPwd
}